package com.atguigu.set;
import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {
    @Test
    public void test(){
        TreeSet set = new TreeSet();
        //例1:
        set.add(123);
        set.add(35);
        set.add(87);
        set.add(18); //输出为18,35,87,123
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
    }
    @Test
    public void test2(){
        Comparator comparator=new Comparator() {
            //按照年龄从小到大排列
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof User&&o2 instanceof User){
                    User u1=(User) o1;
                    User u2=(User) o2;
                    return Integer.compare(u1.getAge(),u2.getAge());
                }else{
                    throw new RuntimeException("类型不同");
                }
            }
        };
        TreeSet set=new TreeSet(comparator);//定制排序
    }
}
